package kr.co.exight.onApp;

import library.Library;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class ActivityTimeList extends Activity {

	/** Called when the activity is first created. */


	public static JSONObject data;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//arrCompanyInfo = ActivityTabOncard.getArrayData();
		setContentView(R.layout.act_timelist);
		
		ListView lstView = (ListView)findViewById(R.id.time_lstList);
		TextView tvNone = (TextView)findViewById(R.id.time_tvNone);
		
		Library.setFontNanumGothic(tvNone);
		
		TestAdapter adapter = new TestAdapter();

		JSONArray arrData = null;
		try {
			arrData = data.getJSONArray("items");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if(arrData.length() == 0){
			tvNone.setVisibility(View.VISIBLE);
		}else{
			tvNone.setVisibility(View.GONE);
			lstView.setAdapter(adapter);
		}
	}


	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

		
	}
	
	
	public static void setJSONData(JSONObject _data){
		data = _data;
	}


	class TestAdapter extends BaseAdapter{

		public int getCount() {

			try {
				return data.getJSONArray("items").length();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return 0;
		}

		public Object getItem(int position) {

			try {
				return data.getJSONArray("items").get(position);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return position;
		}

		public long getItemId(int position) {

			return 0;
		}

		public View getView(final int position, View convertView, ViewGroup parent) {

			View v = convertView;
			if(v == null){
				v = getLayoutInflater().inflate(R.layout.cell_timelist, null);
			}

			final JSONObject a;
			try {
				a = data.getJSONArray("items").getJSONObject(position).getJSONObject("spot");


				TextView tvName = (TextView)v.findViewById(R.id.timeCell_tvName);
				TextView tvTime = (TextView)v.findViewById(R.id.timeCell_tvTime);
				LinearLayout llList = (LinearLayout)v.findViewById(R.id.timeCell_ll);
				ImageView ivClock = (ImageView)v.findViewById(R.id.timeCell_ivClock);
				
				Library.setFontNanumGothic(tvName);
				Library.setFontNanumGothic(tvTime);
				Library.setTextViewBold(tvTime);
				Library.setTextViewBold(tvName);
				
				tvName.setText(a.getString("name"));
				
				
				
				
			String start = 
				data.getJSONArray("items").getJSONObject(position).getJSONObject("time").getString("start");
			String end =
					data.getJSONArray("items").getJSONObject(position).getJSONObject("time").getString("end");
		
			if(Library.isCurrentTimeInThisTime(start, end)){
				ivClock.setBackgroundResource(R.drawable.ico_ontime_on);
			}else {
				ivClock.setBackgroundResource(R.drawable.ico_ontime_off);
			}
				
			
			
			start = start.substring(0, start.length() - 3);
				end = end.substring(0, end.length() - 3);
				
			
				tvTime.setText(start +" ~ " + end);
				
				llList.setOnClickListener(new OnClickListener() {

					public void onClick(View v) {
						// TODO Auto-generated method stub
						ActivityTimeDetail.setJSONData(a);
						Library.startActivity(ActivityTimeList.this, ActivityTimeDetail.class);
					}
				});
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return v;
		}
	}

}
